# -*- coding: utf-8 -*-
# @Time : 2022/8/20 14:48
# @Author : WangYunfei
# @FileName: 美食.py
# @Email: lxb_wyf@163.com
import re

import lxml
import matplotlib
import requests
from fontTools.misc.transform import Offset
from fontTools.pens.freetypePen import FreeTypePen
from fontTools.ttLib import TTFont
from lxml import etree

url = "https://www.dianping.com/nanjing/ch10/g1338r1681"
headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "max-age=0",
    "Connection": "keep-alive",
    "Cookie": "navCtgScroll=0; showNav=#nav-tab|0|0; fspop=test; cy=5; cye=nanjing; s_ViewType=10; _hc.v=5916ced6-e8f9-d74e-1181-39666ae26f5d.1660977664",
    "Host": "www.dianping.com",
    "sec-ch-ua": "\"Chromium\";v=\"104\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"104\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "Sec-Fetch-Dest": "document",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-User": "?1",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
}


# res = requests.get(url=url, headers=headers)
# print(res.text)


def get_one(data_list):
    return data_list[0] if data_list else ""


def get_num(text):
    return re.search(r"\d+", text).group() if re.search(r"\d+", text) else 0


html = etree.HTML(open("./g1338r1681.html", encoding="utf-8").read())
shop_list_html = html.xpath('//*[@id="shop-all-list"]/ul/li')
for shop_html in shop_list_html:
    shop_name = get_one(shop_html.xpath('./div[@class="txt"]/div[@class="tit"]//h4/text()'))
    shop_comment_star = get_num(get_one(shop_html.xpath('.//div[@class="comment"]//div[@class="star_icon"]//span[1]/@class')))
    shop_comment_num = shop_html.xpath('.//a[@class="review-num"]/b//text()')

    print(shop_comment_num)  # div[@class="star_icon"]/span[1]/@class

font = TTFont("./ee552432.woff")
font.saveXML(r"font.xml")
code_name_map = font.getBestCmap()
print(code_name_map)


pen = FreeTypePen(None)  # 实例化Pen子类
glyph = font.getGlyphSet()["uniee06"] # 通过字形名称选择某一字形对象
#glyph.draw(pen) # “画”出字形轮廓
width, ascender, descender = glyph.width, font['OS/2'].usWinAscent, -font['OS/2'].usWinDescent # 获取字形的宽度和上沿以及下沿
height = ascender - descender # 利用上沿和下沿计算字形高度
pen.show(width=width, height=height, transform=Offset(0, -descender))

coordinate = font['glyf']['uniee06'].coordinates
print(coordinate)
#
# # 坐标矩阵对比 来找相似度 可以解决动态字体问题
#
# # 通过坐标描绘一个文字  他是一个图像库
import matplotlib.pyplot as plt
coordinate = list(coordinate)

matplotlib.rc("font",family='Songti SC')
fig, ax = plt.subplots()
x = [i[0] for i in coordinate]
y = [i[1] for i in coordinate]
plt.fill(x, y, color="k", alpha=1)
# 取消边框
for key, spine in ax.spines.items():
     if key == 'right' or key == 'top' or key == 'bottom' or key == 'left':
         spine.set_visible(False)
plt.plot(x, y)
# 取消坐标：
plt.axis('off')
plt.savefig('uniF0D5.png')
plt.show()
